using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Data;
using System.Drawing.Imaging;
namespace DbGraph {
	public class DbGraph {
		static void Main() {

			DAL dal=new DAL("pon-wpsq16-db","directory");

			string sql="select convert(char(16),BonusDate,120)[day],BonusNumber,count(*)[count]from (select top 20000 * from DirBonusLog(nolock)order by RecordID desc)t1 group by convert(char(16),BonusDate,120),BonusNumber order by 1 desc";
			DataTable tab=dal.Query(sql);



			DataProc proc=new DataProc(tab,20);

			string finameName="img.png";
			using(Bitmap bmp=new Bitmap(200,200))
			using(Graphics g=Graphics.FromImage(bmp))
			{
				g.Clear(Color.White);





				bmp.Save(finameName,ImageFormat.Png);
			}
			Process.Start(finameName);
		}
	}
	class DataProc {
		//DataTable _tab;
		Dictionary<string,List<KeyValuePair<int,float>>> dict=new Dictionary<string,List<KeyValuePair<int,float>>>();
		DateTime minDat;
		DateTime maxDat;
		Reso res;
		string datForm="yyyy-MM-dd HH:mm:ss.fff";
		void Add(string date,string key,float val) {
			if(!dict.ContainsKey(key))
				dict.Add(key,new List<KeyValuePair<int,float>>());

			DateTime dat=DateTime.ParseExact(date,datForm,null);
			dict[key].Add(new KeyValuePair<int,float>(GetDiff(dat),val));

		}
		DateTime GetDate(string date) {
			return DateTime.ParseExact(date,datForm,null);
		}
		int GetDiff(DateTime dat) {
			return (int)(dat-minDat).TotalMinutes;
		}
		public DataProc(DataTable tab,int height) {
			//_tab=tab;
			if(tab.Rows.Count==0)
				throw new Exception("no rows!");
			res=(Reso)tab.Rows[0][0].ToString().Length;
			datForm="yyyy-MM-dd HH:mm:ss.fff".Substring(0,(int)res);
			minDat=GetDate(tab.Compute(string.Format("min({0})",tab.Columns[0].ColumnName),null).ToString());
			maxDat=GetDate(tab.Compute(string.Format("max({0})",tab.Columns[0].ColumnName),null).ToString());
			foreach(DataRow row in tab.Rows)
			{
				Add(row[0].ToString(),row[1].ToString(),float.Parse(row[2].ToString()));

			}



		}
		public void Draw(Graphics g) {


		}
	}
	enum Reso {
		Second=19,
		Sec10=18,
		Minute=16,
		Min10=15,
		Hour=13,
		Day=10
	}
}
